clear all
set more off, permanently

********************************************************************************
************************************ Reshape ***********************************
********************************************************************************

use "...\PADDD_impact.dta"

********** rainfall

drop if undis==.

keep ID1 _*prec


reshape long _prec, i(ID1) j(year)

save "...\prec_reshape_TMF.dta", replace

********* temperatures

clear all

use "...\PADDD_impact.dta"

drop if undis==.

keep ID1 tavg20*

reshape long tavg, i(ID1) j(year)

save "...\tavg_reshape_TMF.dta", replace

********* TMFchange

clear all

use "...\PADDD_impact.dta"

drop if undis==.

keep ID1 TMFchange*

reshape long TMFchange, i(ID1) j(year)

save "...\tmfchange_reshape.dta", replace

********* TMF
clear all

use "...\PADDD_impact.dta"

drop if undis==.

keep ID1 TMF_*

reshape long TMF_, i(ID1) j(year)

save "...\tmf_reshape.dta", replace

********** agricultural fires

clear all

use "...\PADDD_impact.dta"

drop if undis==.

keep ID1 agrifires*

reshape long agrifires, i(ID1) j(year)

save "...\agrifires_reshape_TMF.dta", replace

********** nighttime lights

clear all

use "...\PADDD_impact.dta"

drop if undis==.

keep ID1 light*

reshape long light, i(ID1) j(year)

save "...\light_reshape_TMF.dta", replace

********** PAs and PADDD

clear all

use "...\PADDD_impact.dta"

drop if undis==.

keep ID1 PA* PADDD* 

gen _PA1995= PA1959
replace _PA1995= PA1961 if _PA1995==.
replace _PA1995= PA1974 if _PA1995==.
replace _PA1995= PA1978 if _PA1995==.
replace _PA1995= PA1979 if _PA1995==.
replace _PA1995= PA1980 if _PA1995==.
replace _PA1995= PA1981 if _PA1995==.
replace _PA1995= PA1982 if _PA1995==.
replace _PA1995= PA1983 if _PA1995==.
replace _PA1995= PA1984 if _PA1995==.
replace _PA1995= PA1985 if _PA1995==.
replace _PA1995= PA1988 if _PA1995==.
replace _PA1995= PA1989 if _PA1995==.
replace _PA1995= PA1990 if _PA1995==.
replace _PA1995= PA1991 if _PA1995==.
replace _PA1995= PA1992 if _PA1995==.
replace _PA1995= PA1993 if _PA1995==.
replace _PA1995= PA1994 if _PA1995==.
replace _PA1995= PA1995 if _PA1995==.

drop PA1959 PA1961 PA1974 PA1978 PA1979 PA1980 PA1981 PA1984 PA1985 PA1988 PA1989 PA1990 PA1991 PA1992 PA1993 PA1994 PA1995
rename _PA1995 PA1995
drop PA1982 PA1983 

gen _PADDD1995= PADDD1971
replace _PADDD1995= PADDD1988 if _PADDD1995==.
replace _PADDD1995= PADDD1991 if _PADDD1995==.
replace _PADDD1995= PADDD1995 if _PADDD1995==.

drop  PADDD1971 PADDD1988 PADDD1991 PADDD1995
rename _PADDD1995  PADDD1995

rename PADDD PADDD__

reshape long PA PADDD , i(ID1) j(year)

save "...\other_reshape_TMF.dta", replace

//merger avec les autres variables

merge 1:1 ID1 year using  "...\light_reshape_TMF.dta", nogen
merge 1:1 ID1 year using  "...\agrifires_reshape_TMF.dta", nogen
merge 1:1 ID1 year using  "...\tmfchange_reshape.dta", nogen
merge 1:1 ID1 year using  "...\tmf_reshape.dta", nogen
merge 1:1 ID1 year using  "...\tavg_reshape_TMF.dta", nogen
merge 1:1 ID1 year using  "...\prec_reshape_TMF.dta", nogen

save "...\reshape_TMF.dta", replace

********** merge reshaped data with fixed covariates

**** take only fixed covariates 

clear all

use "...\PADDD_impact.dta"

drop if undis==.

keep ID1 PADDD dist_PA dist_PADDD year_PADDD yr_desigPa typePADDD statusPADDD ID_state Rprotected2000* NRprotected2000* enacR_0912* 

save "...\fix_var_TMF.dta", replace

clear all

**** merge with reshaped data

use "...\reshape_TMF.dta"

merge m:1 ID1 using "...\fix_var_TMF.dta"

save "...\DID_TMF.dta", replace

********************************************************************************
*********************** robustness check using TMF *****************************
********************************** BZ 20km *************************************

clear

use "...\DID_TMF.dta"

****************************** prepare data ***********************************

******** redefine treatment variables 

replace enacR_0912=.
replace enacR_0912=1  if dist_PADDD<1 & year_PADDD<2013 & year_PADDD>2008 & statusPADDD=="Enacted" & typePADDD!="downgrade"
replace enacR_0912=0 if !(dist_PADDD<1 & year_PADDD<2013 & year_PADDD>2008 & statusPADDD=="Enacted" & typePADDD!="downgrade") & (yr_desigPa<2009 & dist_PADDD>20 & dist_PA==0)


******** drop unecessary observations
drop if enacR_0912==.
drop if year<2000 
drop if PADDD__==731
drop if PADDD__==723
drop if PADDD__==734

******** PA and PADDD indicators

gen PADDD_=1 if PADDD__!=.
gen PA_=1 if PA!=.

bysort ID1: egen PA0108=max(PA_) if year<2009
bysort ID1: egen PA0108_=max(PA0108) 
drop PA0108
rename PA0108_ PA0108

bysort ID1: egen IDPA=max(PA)
bysort ID1: egen IDPADDD=max(PADDD)

gen ID_PA_PADDD=IDPADDD
replace ID_PA_PADDD=IDPA if ID_PA_PADDD==.

******** PADDD misallocation

replace ID_state=22 if (IDPADDD==696 | IDPADDD==695)

******** import PA designations

merge m:m PADDD using  "C:\Users\dkeles\Documents\these\article2\important\desigPA.dta", gen(mergedesig)
gen desig=1 if Designation=="Federal"
replace desig=1 if Designation=="federal"
replace desig=2 if Designation=="State"
replace desig=2 if Designation=="state"
bysort ID1: fillmissing desig, with(any)
drop mergedesig

gen federal=1 if desig==1
replace federal=0 if desig==2
gen state_=1 if desig==2
replace state_=0 if desig==1

******** redefine treatment variables 

gen enacR_12=1 if enacR_0912==1 & year_PADDD==2012
replace enacR_12=0 if enacR_0912==0
gen R12protected2000=1 if Rprotected2000==1 & year_PADDD==2012
replace R12protected2000=0 if Rprotected2000==0 

gen enacR_10=1 if enacR_0912==1 & year_PADDD==2010
replace enacR_10=0 if enacR_0912==0
gen R10protected2000=1 if Rprotected2000==1 & year_PADDD==2010
replace R10protected2000=0 if Rprotected2000==0 

gen enacR_09=1 if enacR_0912==1 & year_PADDD==2009
replace enacR_09=0 if enacR_0912==0
gen R09protected2000=1 if Rprotected2000==1 & year_PADDD==2009
replace R09protected2000=0 if Rprotected2000==0 

sort ID1 year
replace enacR_12=0 if enacR_0912==1 & year_PADDD==2012 & year>=2000 & year<2012 
sort ID1 year
replace enacR_10=0 if enacR_0912==1 & year_PADDD==2010 & year>=2000 & year<2010 
sort ID1 year
replace enacR_09=0 if enacR_0912==1 & year_PADDD==2009 & year>=2000 & year<2009

gen enacR_10state= enacR_10*state_
replace enacR_10state=0 if enacR_10==0 & enacR_10state==.
replace enacR_10state=. if enacR_10state==0 & enacR_10==0 & state_==.
rename enacR_10state enacRS_10

save  "...\DID_TMF.dta", replace

******** import dataset with matching results

use "...\results_impact_TMF.dta"

keep ID1 *_weight 
drop if ID1==.

******** merge to reshaped panel data

merge 1:m ID1 using "...\DID_TMF.dta", nogen

save "...\DIDmatching_TMF.dta", replace


********************************* DID panel ************************************

clear all

use "...\DIDmatching_TMF.dta"
 
xtset ID1 year

****** Para (ATT)

** ID treated group
bysort ID1: egen enacR_12_=max(enacR_12)

** TWFE
areg TMF_ enacR_12 year agrifires tavg  light _prec  if  PA0108==. & ID_state==14  & (enacR_12==1 | enacR_12==0 & para_weight!=.), absorb(ID1)
areg TMF_ enacR_12 year agrifires tavg  light _prec  if  PA0108==. & ID_state==14  & year>2007 & (enacR_12==1 | enacR_12==0 & para_weight!=.), absorb(ID1)

** DIDl table
did_multiplegt TMF_ ID1 year enacR_12  if  PA0108==. & ID_state==14  & (enacR_12_==1 | (enacR_12_==0 & rdo12PSMp_weight!=.)), ///
robust_dynamic dynamic(3) placebo(10) breps(100) covariances ///
average_effect jointtestplacebo seed(01111995)

** DIDl figure and pretrest test
did_multiplegt TMF_ ID1 year enacR_12  if  PA0108==. & ID_state==14  & (enacR_12_==1 | (enacR_12_==0 & para_weight!=.)), ///
robust_dynamic dynamic(3) placebo(3) breps(100) covariances ///
average_effect jointtestplacebo seed(01111995) longdiff_placebo

****** rondonia (ATU)

** ID treated group
bysort ID1: egen enacR_10_=max(enacR_10)

** TWFE
areg TMF_ enacR_10 year agrifires tavg  light _prec   if PA0108==. & ID_state==22 & (enacR_10_==1 & state_==1 | enacR_10_==0 & state_==.) & (enacR_10_==0 | enacR_10_==1 & rondonia_weight!=.), absorb(ID1)
areg TMF_ enacR_10 year agrifires tavg  light _prec   if PA0108==. & ID_state==22 & (enacR_10_==1 & state_==1 | enacR_10_==0 & state_==.) & (enacR_10_==0 | enacR_10_==1 & rondonia_weight!=.) & year>2007, absorb(ID1)

** DIDl table
did_multiplegt TMF_ ID1 year enacR_10   if PA0108==. & ID_state==22 & (enacR_10_==1 & state_==1 | enacR_10_==0 & state_==.) & (enacR_10_==0 | enacR_10_==1 & rdo10PSM_rs_weight!=.), ///
robust_dynamic dynamic(5) placebo(8) breps(100) covariances ///
average_effect jointtestplacebo seed(01111995)

** DIDl figure and pretrest test
did_multiplegt TMF_ ID1 year enacR_10  if PA0108==. & ID_state==22 & (enacR_10_==1 & state_==1 | enacR_10_==0 & state_==.) & (enacR_10_==0 | enacR_10_==1 & rondonia_weight!=.), ///
robust_dynamic dynamic(5) placebo(5) breps(100) covariances ///
average_effect jointtestplacebo seed(01111995) longdiff_placebo

****** Roraima (ATU)

** ID treated group
bysort ID1: egen enacR_09_=max(enacR_09)

** TWFE
areg TMF_ enacR_09  year agrifires tavg  light _prec if  PA0108==. & ID_state==23 & (enacR_09_==0 | enacR_09_==1 & roraima_weight!=.), absorb(ID1)
areg TMF_ enacR_09  year agrifires tavg  light _prec if  PA0108==. & ID_state==23 & year>2007 & (enacR_09_==0 | enacR_09_==1 & roraima_weight!=.), absorb(ID1)

** DIDl table
did_multiplegt TMF_ ID1 year enacR_09  if  PA0108==. & ID_state==23  & (enacR_09_==0 | enacR_09_==1 & roraima_weight!=.), ///
robust_dynamic dynamic(6) placebo(7) breps(100) covariances ///
average_effect jointtestplacebo seed(01111995)

** DIDl figure and pretrest test
did_multiplegt TMF_ ID1 year enacR_09  if  PA0108==. & ID_state==23  & (enacR_09_==0 | enacR_09_==1 & roraima_weight!=.), ///
robust_dynamic dynamic(6) placebo(6) breps(100) covariances ///
average_effect jointtestplacebo seed(01111995) longdiff_placebo

save "...\DIDmatching_TMF.dta", replace

